package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
/**
 * @author chenxys
 * @Description: 体检套餐管理
 * @date 2022/5/22 15:03
 */
@RestController
@RequestMapping(value = "/setmeal")
public class SetMealController {
    @Reference
    private SetMealService setMealService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 图片文件上传 参数名称与请求参数名称不一致时，使用此注解@RequestParam,文件上传MultipartFile
     * @author chenxys
     * @param imgFile
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try {
            //原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //获得文件后缀                    .xxx                            从这里开始，包括此处
            String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
            //使用uuid产生随机文件名         文件名                 后缀
            String filename = UUID.randomUUID().toString() + extention;
            //使用七牛云工具类上传到七牛云
            QiNiuUtils.upload2QiNiu(imgFile.getBytes(), filename);
            //将上传图片名称存入redis,基于redis的set集合存储
            jedisPool.getResource().sadd(RedisConstant.SET_MEAL_PIC_RESOURCES,filename);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,filename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 添加套餐
     * @author chenxys
     * @param checkGroupIds
     * @param setmeal
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result add(Integer[] checkGroupIds, @RequestBody Setmeal setmeal){
        try {
            setMealService.add(checkGroupIds,setmeal);
        }catch (Exception e){
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     * @author chenxys
     * @param queryPageBean
     * @return com.itheima.entity.PageResult
     */
    @RequestMapping(value = "/pageQuery")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY') " +
            "and hasAuthority('CHECKGROUP_QUERY') " +
            "and hasAuthority('CHECKITEM_QUERY')")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        return setMealService.pageQuery(queryPageBean);
    }

    /**
     * 根据ID删除套餐
     * @author chenxys
     * @param id
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/delete")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Result delete(Integer id){
        try{
            setMealService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 根据ID查询套餐
     * @author chenxys
     * @param id
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/findById")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY') " +
            "and hasAuthority('CHECKGROUP_QUERY') " +
            "and hasAuthority('CHECKITEM_QUERY')")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setMealService.findById(id);
            //查询成功
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 根据套餐ID查询检查组ID
     * @author chenxys
     * @param id
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/findCheckGroupBySetMealId")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY') " +
            "and hasAuthority('CHECKGROUP_QUERY') " +
            "and hasAuthority('CHECKITEM_QUERY')")
    public Result findCheckGroupBySetMealId(Integer id){
        try {
            List<Integer> checkGroupIds = setMealService.findCheckGroupBySetMealId(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,checkGroupIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 套餐编辑
     * @author chenxys
     * @param checkGroupIds
     * @param setmeal
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/edit")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result edit(Integer[] checkGroupIds,@RequestBody Setmeal setmeal){
        try {
            setMealService.edit(checkGroupIds,setmeal);
        }catch (RuntimeException e){
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
}
